(function(angular) {
    "use strict";

    var ctl_module = angular.module("cloapCertificateApp.controller");

    ctl_module.controller("certificate.register.listCtl", function($scope, $modal,
            Certificate, CertificateType, Employee) {
        var modal_instance = null,
            certificate_form = {
                edit: "/cloap/app/cloapCertificateApp/partials/certificate_register_edit.html",
                add: "/cloap/app/cloapCertificateApp/partials/certificate_register_add.html"
            };
        $scope.widget_title = "持证登记";
        $scope.employees = Employee.all();
        $scope.certificate_types = CertificateType.query();
        $scope.certificates = Certificate.query();

        $scope.editOrAdd = function(operate, certificate_id) {
            var ctl = isAdd(operate) ? "certificate.register.addCtl" :
                                       "certificate.register.editCtl";

            modal_instance = $modal.open({
                scope: $scope,
                templateUrl: certificate_form[operate],
                controller: ctl,
                resolve: {
                    params: function() {
                        return {
                            certificate_id: certificate_id
                        };
                    }
                }
            });

            function isAdd(operate) {
                return operate === "add";
            }
        };

        $scope.$on("change_cerficate", function() {
            $scope.certificates = Certificate.query();
        });

        $scope.saveCertificate = function(save_method, data, callback) {
            Certificate[save_method](data, callback, function(response) {
                alert(response.message || "保存失败");
            });
        };

        $scope.deleteCertificate = function(certificate_id) {
            Certificate.delete({certificate_id: certificate_id}, function() {
                $scope.$emit("change_cerficate");
            });
        };

        $scope.closeModal = function() {
            modal_instance.close();
        };
    });

    ctl_module.controller("certificate.register.editCtl", function($scope, Certificate, params) {
        var save_method = "update";

        $scope.certificate =
            Certificate.get({certificate_id: params.certificate_id});
        $scope.widget_title = "编辑证书";

        $scope.save = function() {
            $scope.saveCertificate(save_method, $scope.certificate, saveSuccessCallBack);
        };

        function saveSuccessCallBack() {
            $scope.$emit("change_cerficate");
            $scope.closeModal();
        }
    });

    ctl_module.controller("certificate.register.addCtl", function($scope) {
        var save_method = "save";
        $scope.certificate = {};
        $scope.widget_title = "添加证书";

        $scope.save = function() {
            $scope.saveCertificate(save_method, $scope.certificate, saveSuccessCallBack);
        };

        function saveSuccessCallBack() {
            $scope.$emit("change_cerficate");
            $scope.closeModal();
        }
    });
}(angular));
